﻿MPT.addAction("message_friend", function(_elm) {

    var msg_list_jQ = $('.e_msg_list', _elm);
    var friend_count_jQ = $('.e_friend_count', _elm);
    var notice_count_jQ = $('.e_notice_count', _elm);
    var clear_jQ = $('.e_clear', _elm);
    var page_jQ = $('.e_page', _elm);

    var hint_A = ["", "您已接受该用户的好友请求", "您已忽略了该用户的好友请求", "", ""];
    var friendGroupList;

    var addActionMessage = function(_msg_elm, _data) {
        var this_jQ = $(_msg_elm);
        var agree_jQ = $('.e_agree', _msg_elm);
        var ignore_jQ = $('.e_ignore', _msg_elm);
        var close_jQ = $('.e_close', _msg_elm);
        var operate_jQ = $('.e_operate', _msg_elm);
        var hint_jQ = $('.e_hint', _msg_elm);
        var group_manage_jQ = $('.e_group_manage', _msg_elm);

        var data = { "applyID": _data.applyId };

        if (agree_jQ.length != 0) {
            group_manage_jQ.hide();
        }

        //X掉请求
        close_jQ.click(function() {
            Backend.deleteFriendMsg(data, function(_resp) {
                if (Backend.resultValidate(_resp)) {
                    this_jQ.fadeOut("fast", function() { this_jQ.remove(); });
                    Tip.show("消息已清除");
                } else {
                    Tip.show("清除消息失败");
                }
            }, function() {
                Tip.show("清除消息失败");
            });
        });

        //同意请求
        agree_jQ.click(function() {
            Backend.agreeFriend(data, function(_resp) {
                if (Backend.resultValidate(_resp)) {
                    operate_jQ.hide();
                    group_manage_jQ.show();
                    hint_jQ.addClass('block').html(hint_A[1]);
                } else {
                    Tip.show("接受请求失败");
                }
            }, function() {
                Tip.show("接受请求失败");
            });
        });

        //忽略请求
        ignore_jQ.click(function() {
            Backend.unagreeFriend(data, function(_resp) {
                if (Backend.resultValidate(_resp)) {
                    operate_jQ.hide();
                    hint_jQ.addClass('block').html(hint_A[2]);
                } else {
                    Tip.show("忽略请求失败");
                }
            }, function() {
                Tip.show("忽略请求失败");
            });
        });

        //修改分组、选择分组
        var ginfo = { "groupID": _data.groupId, "groupName": "", "groupList": friendGroupList, "uid": _data.uid };
        var modify_group_jQ = SocialUtil.createModifyGroup(ginfo);
        SocialUtil.addActionFriendGroup(modify_group_jQ, ginfo, function() { Tip.show("分组修改成功"); });
        group_manage_jQ.append(modify_group_jQ);

    };

    var displayMessageList = function(pageNo, pageSize) {
        var data = { "pageNo": pageNo, "pageSize": pageSize };
        Backend.findFriendMsg(data, function(_resp) {
            if (Backend.pageBeanValidate(_resp)) {

                var totalRecoreds = _resp.totalRecoreds;
                var results = _resp.results;
                msg_list_jQ.html('');

                if (totalRecoreds > 0) {
                    friend_count_jQ.html(totalRecoreds > 99 ? '99+' : totalRecoreds).show();
                } else {
                    friend_count_jQ.hide();
                }

                Backend.findFriendGroupsWithCount(null, function(_resp) {
                    if (Backend.resultValidate(_resp)) {
                        friendGroupList = SocialUtil.sortFriendGroup(_resp.data);

                        $.each(results, function(_i, _r) {
                            var msg_jQ = $(MPT.getTmpl('one_message', _r));
                            addActionMessage(msg_jQ, _r);
                            msg_list_jQ.append(msg_jQ);
                        });
                    }
                });

                //显示分页
                var page_param = { "pageNo": _resp.pageNo, "pageSize": _resp.pageSize, "totalRecords": _resp.totalRecoreds, "totalPages": _resp.totalPages };
                var pageBar_jQ = (new Paginate).createPageBar(page_param, function(_click_page) {
                    displayMessageList(_click_page, 20);
                });
                page_jQ.empty().append(pageBar_jQ);

            } else {
                msg_list_jQ.html('目前没有新消息');
            }
        }, function() {
            msg_list_jQ.html('消息列表加载失败');
        });
    }

    //清除未读通知数据
    var deleteAllFriendInfo = function() {
        Backend.deleteUnreadFriendMsg(null, function(_resp) {
            if (Backend.resultValidate(_resp)) {
            }
        });
    }

    //========================================页面初始化========================================

    var initialize = function() {
        deleteAllFriendInfo();
        displayMessageList(1, 20);

        clear_jQ.click(function() {
            Backend.deleteAllFriendsMsg(null, function(_resp) {
                if (Backend.resultValidate(_resp)) {
                    friend_count_jQ.hide();
                    msg_list_jQ.html('目前没有新消息');
                    Tip.show("好友请求消息已清空");
                } else {
                    Tip.show("清空消息失败");
                }
            }, function() {
                Tip.show("清空消息失败");
            });

        });
    };

    initialize();
});

